package com.cloud;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.cloud.exception.CustomException;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.lang3.StringUtils;

import javax.sql.DataSource;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;

/**
 * 数据库表说明文档生成
 *
 * @author yzj
 */
public class DBGenerator {

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + tip + ":");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new CustomException("请输入正确的" + tip + "!");
    }

    public static void Generator(String jdbcUrl, String driverName, String userName, String passWord,
                                 String filePath, String fileName) {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl(jdbcUrl);
        hikariConfig.setDriverClassName(driverName);
        hikariConfig.setUsername(userName);
        hikariConfig.setPassword(passWord);
        // 设置可以获取Tables remarks信息
        hikariConfig.addDataSourceProperty("useInformationSchema", "true");
        hikariConfig.setMinimumIdle(2);
        hikariConfig.setMaximumPoolSize(5);
        DataSource dataSource = new HikariDataSource(hikariConfig);
        // 生成文件配置
        EngineConfig engineConfig = EngineConfig.builder().fileOutputDir(filePath)
                .openOutputDir(false).fileType(EngineFileType.WORD).produceType(EngineTemplateType.freemarker).build();
        String result = scanner("是否忽略表? Y:是,N:否!");
        if (result.equalsIgnoreCase("Y")) {
            // 配置想要忽略的表
            ProcessConfig processConfig = null;
            result = scanner("忽略表? BM:忽略表名,QZ:忽略表前缀,HZ:忽略表后缀!");
            if (result.equalsIgnoreCase("BM")) {
                processConfig = ProcessConfig.builder().ignoreTableName(Arrays
                        .asList(scanner("请输入表名多个,隔开!").split(","))).build();
            } else if (result.equalsIgnoreCase("QZ")) {
                processConfig = ProcessConfig.builder().ignoreTablePrefix(Arrays
                        .asList(scanner("请输入表前缀名(例:test_)多个,隔开!").split(","))).build();
            } else if (result.equalsIgnoreCase("HZ")) {
                processConfig = ProcessConfig.builder().ignoreTableSuffix(Arrays
                        .asList(scanner("请输入表后缀名(例:_test)多个,隔开!").split(","))).build();
            }
            // 生成文档配置（包含以下自定义版本号、描述等配置连接）
            Configuration config = Configuration.builder().version("1.0").description(fileName).dataSource(dataSource)
                    .engineConfig(engineConfig).produceConfig(processConfig).build();
            // 执行生成
            new DocumentationExecute(config).execute();
        } else {
            // 生成文档配置（包含以下自定义版本号、描述等配置连接）
            Configuration config = Configuration.builder().version("1.0").description(fileName).dataSource(dataSource)
                    .engineConfig(engineConfig).build();
            // 执行生成
            new DocumentationExecute(config).execute();
        }

    }

    public static void main(String[] args) {
        String jdbcUrl = "jdbc:mysql://192.168.0.254:3306/tlxx_direct?characterEncoding=utf8&useUnicode=true";
        String driverName = "com.mysql.cj.jdbc.Driver";
        String userName = "root";
        String passWord = "123456";
        // 获取当前操作桌面路径
        File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        // 生成文档路径
        String desktopPath = desktopDir.getAbsolutePath();
        Generator(jdbcUrl, driverName, userName, passWord, desktopPath, scanner("生成Word文档名称"));
    }

}
